package com.lagou.social;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver;
import org.apache.hadoop.hbase.coprocessor.ObserverContext;
import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.CollectionUtils;

import java.io.IOException;
import java.util.List;

public class MyProcessor extends BaseRegionObserver {

    @Override
    public void postDelete(ObserverContext<RegionCoprocessorEnvironment> e, Delete delete, WALEdit edit, Durability durability) throws IOException {
        //获取表
        HTableInterface fnet = e.getEnvironment().getTable(TableName.valueOf("fnet"));
        //获取删除对象
        List<Cell> friends = delete.getFamilyCellMap().get(Bytes.toBytes("friends"));

        //判空，如果为空则直接返回
        if (CollectionUtils.isEmpty(friends)) {
            fnet.close();
            return;
        }
        Cell cell = friends.get(0);
        Delete deletefriend = new Delete(CellUtil.cloneQualifier(cell));
        deletefriend.addColumn(Bytes.toBytes("friends"),CellUtil.cloneRow(cell));
        fnet.delete(deletefriend);
        System.out.println("数据删除成功！！");

        fnet.close();
    }
}
